package com.xdclass.concurrent;

import java.io.IOException;

/**
 * @Author: Timo
 * @Date: 2019/4/25 14:58
 * @Description: 线程状态演示
 */
public class ThreadStateDemo {
    public static void main(String[] args) throws InterruptedException {
//        blockedTest();

        waitingTest();
    }

    public static void runnableTest() {
        // RUNNABLE
        Thread t1 = new Thread(()->{
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        t1.start();
    }

    /**
     * 阻塞测试
     */
    public static void blockedTest() throws InterruptedException {
        Object obj = new Object();
        // RUNNABLE
        Thread t1 = new Thread(()->{
            synchronized (obj) {
                try {
                    Thread.sleep(1000_000_10L);
                    System.out.println(1000_000_10L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();

        Thread.sleep(2000L);

        Thread t2 = new Thread(()->{
            synchronized (obj) {
            }
        });
        t2.start();
    }


    public static void waitingTest() throws InterruptedException {
        Object obj = new Object();
        // RUNNABLE
        Thread tw1 = new Thread(()->{
            synchronized (obj) {
                try {
                   obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        tw1.setName("WAITING_TEST_1");
        tw1.start();

        System.out.println(1000_000_10L);
    }
}
